from django.shortcuts import render
from rest_framework.viewsets import ReadOnlyModelViewSet
# Create your views here.
from areas.models import Area
from rest_framework_extensions.cache.mixins import ListCacheResponseMixin,RetrieveCacheResponseMixin
from rest_framework_extensions.cache.mixins import CacheResponseMixin
from .serializers import AreaSerializer,SubAreaSerializer


class AreaView(CacheResponseMixin,ReadOnlyModelViewSet):
    """
    #省份信息 select * from tb_areas where parent_id is null;

    # 市 select * from tb_areas where parent_id=130000;
    #区县 select * from tb_areas where parent_id=130600;


    # GET   省份信息的url      : areas/infos/          list
    # GET   市/区县的url信息    :areas/infos/pk/        retrieve
    """
    pagination_class = None
    def get_queryset(self):

        # 重写这个方法，根据不同的业务逻辑返回不同的查询数据
        # list retrieve create update 等等都是action
        if self.action == 'list':
            return Area.objects.filter(parent__isnull=True)
        else:
            return Area.objects.all()
    def get_serializer_class(self):

        if self.action == 'list':
            return AreaSerializer
        else:
            return SubAreaSerializer
